<?php

namespace app\home\controller;

use think\Controller;
use think\Db;
use think\Request;

class Goods extends Controller
{
    public function goods()
    {
        return $this->fetch('goods');
    }
    //下拉加载数据
    public function loaddata(Request $request)
    {
        if ($request->isAjax()) {
            $shaixuan = [];
            $fl = $request->param('fl');
            $pp = $request->param('pp');
            $wz = $request->param('wz');
            $page = input('page') ? input('page') : 1;
            $limit = input('limit') ? input('limit') : 10;
            $pinpai = Db::name('goods_pinpai');
            $goods=Db::name("goods")->where('shenhe',1);
            $qiniu_config = cmf_get_option('qiniu');
            // 七牛样式处理
            $q_thumb = "?imageMogr2/auto-orient/thumbnail/!60x120r/gravity/Center/crop/200x200/quality/100/interlace/0";
            if ($fl != "不限") {
                $shaixuan['fenlei'] = ['=', $fl];
            }
            if ($wz != "不限") {
                $shaixuan['weizhi'] = ['=', $wz];
            }
            if ($shaixuan) {
                if ($pp == "最新发布") {
                    $res = $goods->where($shaixuan)->order('create_time', 'desc')->page($page, $limit)->select();
                }else{
                    $res = $goods->where($shaixuan)->order('id', 'asc')->page($page, $limit)->select();
                }
            } else {
                if ($pp == "最新发布") {
                    $res = $goods->order('create_time', 'desc')->page($page, $limit)->select();
                }else{
                    $res = $goods->order('create_time', 'asc')->page($page, $limit)->select();
                }
            }
                foreach ($res as $index => $re) {
                    if (!$res[$index]['thumb']) {
                        $res[$index]['thumb'] = "null";
                    } else {
                        $res[$index]['thumb'] = $qiniu_config['domain'] . '/' . $re['thumb'] . $q_thumb;
                    }
                    $pinpai_data = $pinpai->where('id', $re['pinpai_id'])->field('pinpai')->find();
                    if (!$pinpai_data) {
                        $pinpai_data = ['pinpai' => '无'];
                    }
                    $res[$index]['pinpai'] = $pinpai_data;
                }

            return json(['code' => 1, 'msg' => 'ok', 'count' => count($res), 'data' => $res]);
        }
    }

//    搜索
    public function sousuo()
    {
        return $this->fetch('sousuo');
    }

    public function search(Request $request)
    {
        if ($request->isAjax()) {
            $page = input('page') ? input('page') : 1;
            $limit = input('limit') ? input('limit') : 10;
            $fenlei = $request->param('shuju');
            $pp = Db::name('goods_pinpai');
            $goods=Db::name("goods")->where('shenhe',1);
            if($fenlei){
                $res =$goods->where('name', 'like', '%' . $fenlei . '%')->page($page, $limit)->order('create_time', 'desc')->select();
//                $res = Db::name('goods')
//                    ->where([
//                        'name'=>['like', '%' . $fenlei . '%','or'],
//                        'fenlei'=>['like', '%' . $fenlei . '%']
//                    ])
//                    ->page($page, $limit)->order('create_time', 'desc')->select();
                if ($res) {
                    $qiniu_config = cmf_get_option('qiniu');
                    // 七牛样式处理
                    $q_thumb = "?imageMogr2/auto-orient/thumbnail/!60x120r/gravity/Center/crop/200x200/quality/100/interlace/0";
                    foreach ($res as $index => $re) {
                        if (!$res[$index]['thumb']) {
                            $res[$index]['thumb'] = "null";
                        } else {
                            $res[$index]['thumb'] = $qiniu_config['domain'] . '/' . $re['thumb'] . $q_thumb;
                        }
                        $pinpai_data = $pp->where('id', $re['pinpai_id'])->field('pinpai')->find();
                        if (!$pinpai_data) {
                            $pinpai_data = ['pinpai' => '无'];
                        }
                        $res[$index]['pinpai'] = $pinpai_data;
                    }
                    return json(['code' => 1, 'msg' => 'ok', 'count' => count($res), 'data' => $res]);
                }
            }
        }
    }

    public function goods_details(Request $request)
    {
        //获取id值
        $id = $request->param('id');
        if ($request->isGet()) {
            if (!$id) {
                $this->error('id 异常');
            }
            $qiniu_config = cmf_get_option('qiniu');
            // 七牛样式处理
            $q_thumb = "?imageMogr2/auto-orient/thumbnail/!60x120r/gravity/Center/quality/100/interlace/0";
            $res = Db::name('goods')->find($id);
            if (!$res['thumb']) {
                $res['thumb'] = "null";
            } else {
                $res['thumb'] = $qiniu_config['domain'] . '/' . $res['thumb'] . $q_thumb;
            }
            $pp = Db::name('goods_pinpai');
            $sj = Db::name('user_biz');
            $pinpai_data = $pp->where('id', $res['pinpai_id'])->field('pinpai')->find();
            if (!$pinpai_data) {
                $pinpai_data = ['pinpai' => '无'];
            }
            $sj_data = $sj->where('uid', $res['shop_uid'])->field('shop_name')->find();
            if (!$sj_data) {
                $sj_data = ['shop_name' => '无'];
            }
            $res['pinpai'] = $pinpai_data;
            $res['shangjia'] = $sj_data;
            $this->assign('item', $res);
            $this->assign('uid',$res['shop_uid']);
            $this->assign('id',$res['id']);
            return $this->fetch('goods_details');
        }

    }

    public function discount(Request $request)
    {
        if ($request->isGet()) {
            $discount_price = $request->param('discountMoney');
            $count = $request->param('count');
            $this->assign('pp', $discount_price);
            $this->assign('cc', $count);
            $uid = $request->param('uid');
            $id = $request->param('id');
            if (!$uid) {
                $this->error('uid 异常');
            }
            $sj_data = Db::name('user_biz')->where('uid', $uid)->find();
            $res = Db::name('goods')->where('id', $id)->find();
            $rr = $request->domain();
            $this->assign('item', $sj_data);
            $this->assign('url', $rr . url('home/goods/sj_discount', ['id' => $res['id'],
                    'uid' => $res['shop_uid'], 'pinpai_id' => $res['pinpai_id'],'count'=>$count,'discountMoney'=>$discount_price]));
            return $this->fetch('discount');
        }
    }

    public function sj_discount(Request $request)
    {
        $id = $request->param('id');
        $uid = $request->param('uid');
        $pinpai_id = $request->param('pinpai_id');
        $discount_price = $request->param('discountMoney');
        $count = $request->param('count');
        $pp = Db::name('goods_pinpai')->where('id', $pinpai_id)->field('pinpai')->find();
        $sj_data = Db::name('user_biz')->where('uid', $uid)->field('shop_name')->find();
        $res = Db::name('goods')->where('id', $id)->find();
        $qiniu_config = cmf_get_option('qiniu');
        // 七牛样式处理
        $q_thumb = "?imageMogr2/auto-orient/thumbnail/!60x120r/gravity/Center/quality/100/interlace/0";
        if (!$res['thumb']) {
            $res['thumb'] = "null";
        } else {
            $res['thumb'] = $qiniu_config['domain'] . '/' . $res['thumb'] . $q_thumb;
        }
        $this->assign('item', $res);
        $this->assign('sj', $sj_data);
        $this->assign('pp', $pp);
        $this->assign('dprice', $discount_price);
        $this->assign('cc', $count);
        $this->assign('url',url('yh_details',['id'=>$res['id'],'pinpai_id'=>$res['pinpai_id'],
            'uid' => $res['shop_uid'],'count'=>$count,'discountMoney'=>$discount_price]));
        return $this->fetch('sj_discount');
    }
    public function yh_details(Request $request){
        $id = $request->param('id');
        $uid = $request->param('uid');
        $pinpai_id = $request->param('pinpai_id');
        $discount_price = $request->param('discountMoney');
        $count = $request->param('count');
//        $shuju['count']=['=',$count];
//        $shuju['discount_price']=['=',$discount_price];

        $pp = Db::name('goods_pinpai')->where('id', $pinpai_id)->field('pinpai')->find();
        $sj_data = Db::name('user_biz')->where('uid', $uid)->field('shop_name')->find();
        $res = Db::name('goods')->where('id', $id)->find();
        $time['create_time'] = time();
        $bh['order_number']=cmf_get_order_sn();
        $data = ['pinpai' =>$pp['pinpai'], 'shop_name' =>$sj_data['shop_name'] ,'name'=>$res['name'],'xinghao'=>$res['xinghao'],
              'caizhi'=>$res['caizhi'],'discount'=>$res['discount'],'create_time'=>$time['create_time'],
            'order_number'=> $bh['order_number'],'count'=> $count,'discount_price'=>$discount_price];
        $rr=Db::name('goods_discount')->insertGetId($data);
        if($rr){
            $this->assign('item',$res);
            return $this->fetch('yh_details');
        }
    }
//    商品管理
    public function management(){
        if (request()->isAjax()) {
            $page = input('page') ? input('page') : 1;
            $limit = input('limit') ? input('limit') : 10;
            $gd = Db::name('goods');
            $uid=session('uid');
            $res=$gd->page($page, $limit)->where('shop_uid',$uid)->order('id','desc')->select();
//            $res=$gd->page($page, $limit)->order('id','desc')->select();
//            $res = Db::name('goods')
//                ->alias('a')
//                ->join('goods_pinpai b', 'a.pinpai_id = b.id')
//                ->field('*, b.id as pid,a.id as gid')
//                ->page($page, $limit)->select();
            $pp=Db::name('goods_pinpai');
            $sj=Db::name('user_biz');
            $qiniu_config = cmf_get_option('qiniu');
            // 七牛样式处理
            $q_thumb = "?imageMogr2/auto-orient/thumbnail/!60x120r/gravity/Center/quality/100/interlace/0";
            foreach ($res as $index => $re) {

                $res[$index]['create_time'] = date('Y-m-d H:i:s', $re['create_time']);
                if( !$re['thumb']){
                    $re['thumb']='null';
                    $res[$index]['thumb']=$re['thumb'];
                }else{
                    $res[$index]['thumb'] = $qiniu_config['domain'] . '/' . $re['thumb'] . $q_thumb;
                }
                $pinpai_data=$pp->where('id',$re['pinpai_id'])->field('pinpai')->find();
                if(!$pinpai_data){
                    $pinpai_data=['pinpai'=>'无'];
                }
                $sj_data=$sj->where('uid',$re['shop_uid'])->field('shop_name')->find();
                if(!$sj_data){
                    $sj_data=['shop_name'=>'无'];
                }
                $res[$index]['pinpai']=$pinpai_data;
                $res[$index]['shangjia']=$sj_data;
            }
            return json(['code' => 1, 'msg' => 'ok', 'count' => count($res), 'data' => $res]);

        } else {
            $count = Db::name('goods')->count();
            $this->assign('count', $count);
            return $this->fetch('management');
        }
    }

//    编辑商品
    public function edit(Request $request){
        if ($request->isGet()) {
            $id = $request->param('id');
            if (!$id) {
                $this->error('id 异常');
            }
            $res = Db::name('goods')->find($id);
            $this->assign('id',$id);
            $this->assign('item', $res);
            return $this->fetch('edit');
        }

    }
    public function edit_next(Request $request)
    {
        $qiniu_config = cmf_get_option('qiniu');
        // 七牛样式处理
        $q_thumb = "?imageMogr2/auto-orient/thumbnail/!60x120r/gravity/Center/quality/100/interlace/0";
        $pp=Db::name('goods_pinpai');
        if ($request->isGet()) {
            $id = $request->param('id');
            if (!$id) {
                $this->error('id 异常');
            }
            $res = Db::name('goods')->find($id);
            $pinpai_data=$pp->where('id',$res['pinpai_id'])->field('pinpai')->find();
            if(!$pinpai_data){
                $pinpai_data=['pinpai'=>'无'];
            }
            if(!$res['remarks']){
                $res['remarks']="无";
            }
            if( !$res['thumb']){
                $res['thumb']='null';
            }else{
                $res['thumb'] = $qiniu_config['domain'] . '/' . $res['thumb'] . $q_thumb;
            }
            $res['pinpai']=$pinpai_data;
            $this->assign('item', $res);
            $this->assign('id', $id);
            if(!cmf_is_wechat()){
                $this->error('需要微信中打开');
            }


            $weixin = new Weixin();

            $url = request()->domain() . $this->request->url();
            $js_config = $weixin->js_config($url);
            $this->assign('jsconfig', $js_config);
        }

        return $this->fetch('edit_next');
    }
//    编辑页品牌选择
    public function pinpai_edit(Request $request){
    if($request->isAjax()){
        $res=Db::name('goods_pinpai')->field('pinpai')->select();
        return json(['code' => 1, 'msg' => 'ok', 'count' => count($res), 'data' => $res]);
    }
    return $this->fetch('edit_next');
}
//更新数据
    public function editsj(Request $request)
    {
        if($request->isAjax()) {
            $id = $request->param('id');
            $pinpai = $request->param('pinpai');
            $data=input('');
            $pp_id=Db::name('goods_pinpai')->where('pinpai',$pinpai)->value('id');
            $data['pinpai_id']=$pp_id;
            $data['shenhe']=0;
            unset($data['pinpai']);
            $res = Db::name('goods')->where('id', $id)->update($data);
            if($res){
                $this->success("操作成功");
            }
        }
    }
    //添加商品
    public function add()
    {
           return $this->fetch('add');
    }
    //添加商品
    public function add_next(Request $request)
    {
        if($request->isAjax()){
            $res=Db::name('goods_pinpai')->field('pinpai')->select();
            return json(['code' => 1, 'msg' => 'ok', 'count' => count($res), 'data' => $res]);
        }
        if(!cmf_is_wechat()){
            $this->error('需要微信中打开');
        }


        $weixin = new Weixin();

        $url = request()->domain() . $this->request->url();
        $js_config = $weixin->js_config($url);
        $this->assign('jsconfig', $js_config);
        return $this->fetch('add_next');
    }
//    添加商品数据存入数据库
    public function addsj(Request $request)
    {
        if($request->isAjax()) {
//            $name = $request->param('name');
//            $fenlei = $request->param('fenlei');
//            $xinghao = $request->param('xinghao ');
//            $caizhi = $request->param('caizhi');
//            $discount = $request->param('discount');
//            $weizhi = $request->param('weizhi');
            $pinpai = $request->param('pinpai');
            $thumb = $request->param('thumb');
//            $remarks = $request->param('remarks');
            $data=input('');
            $pp_id=Db::name('goods_pinpai')->where('pinpai',$pinpai)->value('id');
//            $data = ['name' =>$name, 'fenlei' => $fenlei ,'caizhi'=> $caizhi,'xinghao'=> $xinghao,
//                'discount'=>$discount,'weizhi'=>$weizhi, 'pinpai'=> $pp,'remarks'=> $remarks,'pinpai_id'=>$pp,'shenhe'=>0];
            $data['pinpai_id']=$pp_id;
            $data['thumb']=$thumb;
            $data['shenhe']=0;
            $data['create_time']=time();
            unset($data['pinpai']);
            $rr=Db::name('goods')->insertGetId($data);
            if($rr){
                $this->success("操作成功");
            }
       }
    }
//    删除商品
    public function del(Request $request)
    {
        if($request->isAjax()){
            $id=$request->param('id');
//            dump($id);
            $res = Db::name('goods')->where('id', $id)->delete();
            if ($res) {
               $this->success('删除成功');
            }
      }

    }
}